#include "flowplot.h"

#include <QPainter>
#include <QTime>
#include <qwt/qwt_plot_layout.h>
#include <qwt/qwt_plot_curve.h>
#include <qwt/qwt_scale_draw.h>
#include <qwt/qwt_scale_widget.h>
#include <qwt/qwt_plot_canvas.h>

class ScaleDraw: public QwtScaleDraw
{
public:
    ScaleDraw(){}
    virtual QwtText label( double ) const
    {
        return QString();
    }
};

class Background: public QwtPlotItem
{
public:
    Background()
    {
        setZ( 0.0 );
    }

    virtual int rtti() const
    {
        return QwtPlotItem::Rtti_PlotUserItem;
    }

    virtual void draw( QPainter *painter,
        const QwtScaleMap &, const QwtScaleMap &,
        const QRectF &canvasRect ) const {
        painter->fillRect(canvasRect,Qt::white);
    }
};

FlowPlot::FlowPlot( QWidget *parent ):
    QwtPlot( parent ),
    dataCount( 0 )
{
    setAutoReplot( false );

    canvas()->setBorderRadius( 10 );

    plotLayout()->setAlignCanvasToScales( true );

//    QwtLegend *legend = new QwtLegend;
//    legend->setItemMode( QwtLegend::CheckableItem );
//    insertLegend( legend, QwtPlot::RightLegend );

//    setAxisTitle( QwtPlot::xBottom, " System Uptime [h:m:s]" );
    setAxisScaleDraw( QwtPlot::xBottom,new ScaleDraw );
    setAxisScale( QwtPlot::xBottom, 0, HISTORY );
//    setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
//    setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
    setAxisFont(QwtPlot::yLeft,QFont("Verdana",6));

    /*
     In situations, when there is a label at the most right position of the
     scale, additional space is needed to display the overlapping part
     of the label would be taken by reducing the width of scale and canvas.
     To avoid this "jumping canvas" effect, we add a permanent margin.
     We don't need to do the same for the left border, because there
     is enough space for the overlapping label below the left scale.
     */

//    QwtScaleWidget *scaleWidget = axisWidget( QwtPlot::xBottom );
//    const int fmh = QFontMetrics( scaleWidget->font() ).height();
//    scaleWidget->setMinBorderDist( 0, fmh / 2 );

//    setAxisTitle( QwtPlot::yLeft, "Bytes [Kb]" );
//    setAxisScale( QwtPlot::yLeft, 0,0.0000001);

    Background *bg = new Background();
    bg->attach( this );

//    CpuPieMarker *pie = new CpuPieMarker();
//    pie->attach( this );

//    CpuCurve *curve;

    curve = new FlowCurve( "" );
    curve->setColor( Qt::blue );
    curve->attach( this );
//    data[System].curve = curve;

//    curve = new CpuCurve( "User" );
//    curve->setColor( Qt::blue );
//    curve->setZ( curve->z() - 1 );
//    curve->attach( this );
//    data[User].curve = curve;

//    curve = new CpuCurve( "Total" );
//    curve->setColor( Qt::black );
//    curve->setZ( curve->z() - 2 );
//    curve->attach( this );
//    data[Total].curve = curve;

//    curve = new CpuCurve( "Idle" );
//    curve->setColor( Qt::darkCyan );
//    curve->setZ( curve->z() - 3 );
//    curve->attach( this );
//    data[Idle].curve = curve;

    curve->setVisible(true);
    replot();
//    showCurve( curve, true );
//    showCurve( data[User].curve, true );
//    showCurve( data[Total].curve, false );
//    showCurve( data[Idle].curve, false );

    for ( int i = 0; i < HISTORY; i++ ){

        timeData[HISTORY - 1 - i] = i;
        data[i] = 0;

    }

//    ( void )startTimer( 250 ); // 1 second

//    connect( this, SIGNAL( legendChecked( QwtPlotItem *, bool ) ),
//        SLOT( showCurve( QwtPlotItem *, bool ) ) );
}

void FlowPlot::timerEvent( QTimerEvent * )
{
    for ( int i = dataCount; i > 0; i-- ){
//        for ( int c = 0; c < NCpuData; c++ )
//        {
            if ( i < HISTORY )
                /*data[c].*/data[i] = /*data[c].*/data[i-1];
//        }
    }

//    cpuStat.statistic( data[User].data[0], data[System].data[0] );

//    data[Total].data[0] = data[User].data[0] + data[System].data[0];
//    data[Idle].data[0] = 100.0 - data[Total].data[0];

    if ( dataCount < HISTORY )
        dataCount++;

    for ( int j = 0; j < HISTORY; j++ )
        timeData[j]++;

    setAxisScale( QwtPlot::xBottom,
        timeData[HISTORY - 1], timeData[0] );

//    for ( int c = 0; c < NCpuData; c++ )
//    {
        /*data[c].*/curve->setRawSamples(timeData, /*data[c].*/data, dataCount);
//    }

    replot();
}

void FlowPlot::setTitle(const QString & title)
{
    setAxisTitle(QwtPlot::yLeft,title);
}
